﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform.SG;

namespace Biz.Buffer
{
    public static class CommonInformation
    {
        private static DateTime _lastLoadTime = DateTime.Now;
        private static List<企业排口> _letports;
        private static int BufferTimespan = 60;

        public static 企业排口 GetLetportByMn(string mn)
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > BufferTimespan)
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _letports = pCtx.企业排口.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports?.FirstOrDefault(t => t.MN号.Contains(mn));
        }

        public static 企业排口 GetLetportById(int id)
        {
            if (_letports == null || (DateTime.Now - _lastLoadTime).TotalMinutes > BufferTimespan)
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _letports = pCtx.企业排口.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _letports?.FirstOrDefault(t => t.ID == id);
        }

        private static List<企业基本信息> _pollutionSources;

        public static 企业基本信息 GetPollutionSourceById(int id)
        {
            if (_pollutionSources == null || (DateTime.Now - _lastLoadTime).TotalMinutes > BufferTimespan)
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _pollutionSources = pCtx.企业基本信息.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            return _pollutionSources?.FirstOrDefault(t => t.ID == id);
        }

        private static List<污染物类别> _contamTypes;
        public static 污染物类别 GetContamType(string code)
        {
            if (_contamTypes == null || (DateTime.Now - _lastLoadTime).TotalMinutes > BufferTimespan)
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _contamTypes = pCtx.污染物类别.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }
            var name = "污水";
            if (code.Contains("B01")) name = "污水";
            else if (code.Contains("B02")) name = "废气";

            return _contamTypes?.FirstOrDefault(t => t.类别名称.Contains(name));
        }

        private static List<污染物编码> _contamCodes;


        public static 污染物编码 GetContamCode(string code)
        {
            if (_contamCodes == null || (DateTime.Now - _lastLoadTime).TotalMinutes > BufferTimespan)
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _contamCodes = pCtx.污染物编码.AsNoTracking().ToList();
                }
                _lastLoadTime = DateTime.Now;
            }

            return _contamCodes?.FirstOrDefault(t => t.污染物代码.Contains(code));
        }
    }
}
